using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using System.ComponentModel;
using System.Xml.Serialization;
using System.IO;
using System.Xml;
using System.Web.UI.WebControls;

namespace TST.SharePoint.Shared
{
    /// <summary>
    /// XML serializable object used in WebPart to store/read configuration of a
    /// Datasource.
    /// </summary>
    [Serializable()]
    public class DatasourceConfiguration
    {
        private string _datasourceID;
        private string _itemmenu;
        private bool _showItemMenuLink;
        private bool _showWebMenuLink;
        private bool _showListMenuLink;
        private bool _showFolderMenuLink;
        private string _groupBy;
        private string _groupMode;
        private string _sortBy;
        private string _sortOrder;
        private int _pageSize;
        private int _maximumItems;
        private CustomProperties _customProperties;
        private FieldReferences _fields;
        private Filters _filters;
        private GridPagerMode _gridPagerMode;
        private PagerPosition _pagerPosition;
        private int _pageButtonCount;
        private GridTypes _gridType;
        private bool _justSearchOnQuery;
        private Boolean _hideGroupFieldName;
        private bool _filterInverse;

        /// <summary>
        /// Constructor.
        /// </summary>
        public DatasourceConfiguration()
        {
            _customProperties = new CustomProperties();
            _filters = new Filters();
            _groupBy = string.Empty;
            _groupMode = string.Empty;
            _pagerPosition = PagerPosition.Bottom;
            _gridPagerMode = GridPagerMode.NumericButtons;
        }

        public string DatasourceID
        {
            get { return _datasourceID; }
            set { _datasourceID = value; }
        }

        /// <summary>
        /// Get or set GroupBy. Returns an empty Guid is no GroupBy was set.
        /// </summary>
        public string GroupBy
        {
            get
            {
                return _groupBy;
            }
            set
            {
                if (value == Guid.Empty.ToString("B"))
                    _groupBy = string.Empty;
                else
                    _groupBy = value;
            }
        }

        /// <summary>
        /// Get or set GroupMode. Returns "Extended" is no GroupMode was set.
        /// </summary>
        public string GroupMode
        {
            get
            {
                if (string.IsNullOrEmpty(_groupMode))
                    return "Extended";
                else
                    return _groupMode;
            }
            set { _groupMode = value; }
        }

        public string ItemMenu
        {
            get
            {
                return _itemmenu;
            }
            set
            {
                if (value == Guid.Empty.ToString("B"))
                    _itemmenu = string.Empty;
                else
                    _itemmenu = value;
            }
        }

        public bool ShowItemMenuLink
        {
            get { return _showItemMenuLink; }
            set
            {
                _showItemMenuLink = value;
            }
        }

        public bool ShowWebMenuLink
        {
            get { return _showWebMenuLink; }
            set
            {
                _showWebMenuLink = value;
            }
        }

        public bool ShowListMenuLink
        {
            get { return _showListMenuLink; }
            set
            {
                _showListMenuLink = value;
            }
        }

        public bool ShowListFolderLink
        {
            get { return _showFolderMenuLink; }
            set
            {
                _showFolderMenuLink = value;
            }
        }

        public Boolean CustomPaging
        {
            get;
            set;
        }

        /// <summary>
        /// Get or set SortBy. Returns an empty Guid is no SortBy was set.
        /// </summary>
        public string SortBy
        {
            get
            {
                return _sortBy;
            }
            set
            {
                if (value == Guid.Empty.ToString("B"))
                    _sortBy = string.Empty;
                else
                    _sortBy = value;
            }
        }

        /// <summary>
        /// Get or set SortOrder. Returns "asc" if no sortorder was set.
        /// </summary>
        public string SortOrder
        {
            get
            {
                if (string.IsNullOrEmpty(_sortOrder))
                    return "asc";
                else
                    return _sortOrder;
            }
            set { _sortOrder = value; }
        }

        /// <summary>
        /// Get or set the pagesize.
        /// </summary>
        public int PageSize
        {
            get { return _pageSize; }
            set { _pageSize = value; }
        }

        /// <summary>
        /// Get or set the maximum number of items.
        /// </summary>
        public int MaximumItems
        {
            get { return _maximumItems; }
            set { _maximumItems = value; }
        }

        public GridPagerMode PagerMode
        {
            get
            {
                return _gridPagerMode;
            }
            set
            {
                if (_gridPagerMode == value)
                    return;
                _gridPagerMode = value;
            }
        }
        public PagerPosition PagerPosition
        {
            get
            {
                return _pagerPosition;
            }
            set
            {
                if (_pagerPosition == value)
                    return;
                _pagerPosition = value;
            }
        }
        public int PageButtonCount
        {
            get
            {
                return _pageButtonCount;
            }
            set
            {
                if (_pageButtonCount == value)
                    return;
                _pageButtonCount = value;
            }
        }

        public GridTypes GridType
        {
            get
            {
                return _gridType;
            }
            set
            {
                _gridType = value;
            }
        }

        public bool JustSearchOnQuery
        {
            get { return _justSearchOnQuery; }
            set
            {
                _justSearchOnQuery = value;
            }
        }

        public bool FilterInverse
        {
            get { return _filterInverse; }
            set
            {
                _filterInverse = value;
            }
        }

        public bool HideGroupFieldName
        {
            get { return _hideGroupFieldName; }
            set { _hideGroupFieldName = value; }
        }

        private string _startDateField;
        public string StartDateField
        {
            get
            {
                return _startDateField;
            }
            set
            {
                if (value == Guid.Empty.ToString("B"))
                    _startDateField = string.Empty;
                else
                    _startDateField = value;
            }
        }

        private string _endDateField;
        public string EndDateField
        {
            get
            {
                return _endDateField;
            }
            set
            {
                if (value == Guid.Empty.ToString("B"))
                    _endDateField = string.Empty;
                else
                    _endDateField = value;
            }
        }

        private string _titleField;
        public string TitleField
        {
            get
            {
                return _titleField;
            }
            set
            {
                if (value == Guid.Empty.ToString("B"))
                    _titleField = string.Empty;
                else
                    _titleField = value;
            }
        }

        private string _locationField;
        public string LocationField
        {
            get
            {
                return _locationField;
            }
            set
            {
                if (value == Guid.Empty.ToString("B"))
                    _locationField = string.Empty;
                else
                    _locationField = value;
            }
        }

        private string _descriptionField;
        public string DescriptionField
        {
            get
            {
                return _descriptionField;
            }
            set
            {
                if (value == Guid.Empty.ToString("B"))
                    _descriptionField = string.Empty;
                else
                    _descriptionField = value;
            }
        }

        private string _isAllDayField;
        public string IsAllDayField
        {
            get
            {
                return _isAllDayField;
            }
            set
            {
                if (value == Guid.Empty.ToString("B"))
                    _isAllDayField = string.Empty;
                else
                    _isAllDayField = value;
            }
        }

        private bool _openAllLinksInNewWindows;
        public bool OpenAllLinksInNewWindows
        {
            get { return _openAllLinksInNewWindows; }
            set
            {
                _openAllLinksInNewWindows = value;
            }
        }

        /// <summary>
        /// Get or set the CustomProperties. These are depending on the datasource implementation 
        /// and specific for that datasource.
        /// </summary>
        [XmlArray("CustomProperties"), XmlArrayItem("CustomProperty", typeof(CustomProperty))]
        public CustomProperties CustomProperties
        {
            get { return _customProperties; }
            set { _customProperties = value; }
        }

        [XmlArray("Filters"), XmlArrayItem("Filter", typeof(Filter))]
        public Filters Filters
        {
            get { return _filters; }
            set { _filters = value; }
        }

        /// <summary>
        /// Get or set a list holding references to field objects.
        /// </summary>
        [XmlArray("FieldReferences"), XmlArrayItem("FieldReference", typeof(FieldReference))]
        public FieldReferences Fields
        {
            get
            {
                if (_fields == null)
                    _fields = new FieldReferences();
                return _fields;
            }
            set { _fields = value; }
        }

        public static DatasourceConfiguration ReadXml(string configuration)
        {
            if (string.IsNullOrEmpty(configuration))
            {
                return new DatasourceConfiguration();
            }
            XmlSerializer serializer = new XmlSerializer(typeof(DatasourceConfiguration));
            MemoryStream stream = new MemoryStream(StringToUTF8ByteArray(configuration));
            XmlTextWriter writer = new XmlTextWriter(stream, Encoding.UTF8);
            DatasourceConfiguration conf = (DatasourceConfiguration)serializer.Deserialize(stream);
            return conf;
        }

        private static string UTF8ByteArrayToString(byte[] p)
        {
            UTF8Encoding encoding = new UTF8Encoding();
            string result = encoding.GetString(p);
            return result;
        }

        private static byte[] StringToUTF8ByteArray(string p)
        {
            UTF8Encoding encoding = new UTF8Encoding();
            byte[] result = encoding.GetBytes(p);
            return result;
        }


        public string WriteXml()
        {
            XmlSerializer serializer = new XmlSerializer(typeof(DatasourceConfiguration));
            System.IO.MemoryStream stream = new System.IO.MemoryStream();
            XmlTextWriter writer = new XmlTextWriter(stream, Encoding.UTF8);
            serializer.Serialize(writer, this);
            stream = (System.IO.MemoryStream)writer.BaseStream;
            return UTF8ByteArrayToString(stream.ToArray());
        }

        public enum GridPagerMode
        {
            NextPreviousImages,
            NextPreviousButtons,
            NextPreviousFirstLastImages,
            NextPreviousFirstLastButtons,
            NumericButtons,
            NumericFirstLastButtons
        }

        public enum GridTypes
        {
            SharePointGrid,
            SharePointCalendar,
            SharePointGantt
        }
    }
}
